/*
 * @Author: xiaoty4 xiaoty4@midea.com
 * @Date: 2022-05-27 13:19:24
 * @LastEditors: Rays77
 * @LastEditTime: 2022-10-24 15:01:39
 * @FilePath: \shop-admin\src\store\cacheViewStore\index.ts
 * @Description: 缓存路由配置
 */
import { defineStore } from 'pinia'
import { LocationQuery, RouteMeta } from 'vue-router'

interface CacheData {
  name: string
  meta: RouteMeta
  path: string
  query: LocationQuery
}

export const useCacheViewStore = defineStore('cacheViewStore', {
  state: () => {
    return {
      cacheView: [] as Array<string> // 保存需要缓存的数组
    }
  },
  getters: {},
  actions: {
    setCacheView(data: CacheData) {
      if (this.cacheView.includes(data.name)) return
      if (!data.meta.noCache && data.path) {
        this.cacheView.push(data.name)
      }
    },
    clearCacheView(viewName?: string) {
      if (viewName) {
        const index = this.cacheView.findIndex((item) => item === viewName)
        this.cacheView.splice(index, 1)
      } else {
        this.cacheView = []
      }
    }
  }
})
